package com.rhino.xchat.http.request

import com.blankj.utilcode.util.DeviceUtils
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException


/**
 * @author LuoLin
 * @since Create on 2025/4/1.
 */
class ReqLogin(
    var phone: String,
    var password: String? = null,
    var code: String? = null,
    var sensor: String = DeviceUtils.getAndroidID()
) : BaseRequest() {
    companion object {
        fun encodePassword(input: String): String {
            try {
                // 1. 创建 MessageDigest 实例（指定算法为 MD5）
                val md = MessageDigest.getInstance("MD5");

                // 2. 将输入字符串转换为字节数组（需指定编码，如 UTF-8）
                val inputBytes = input.toByteArray(StandardCharsets.UTF_8);

                // 3. 计算哈希值
                val hashBytes = md.digest(inputBytes);

                // 4. 将字节数组转换为十六进制字符串
                val hexString = StringBuilder()
                for (b in hashBytes) {
                    val hex = String.format("%02x", b);
                    hexString.append(hex);
                }

                return hexString.toString();
            } catch (e: NoSuchAlgorithmException) {
                // MD5 算法在所有 Java 平台必须支持，此处异常理论上不会发生
                throw RuntimeException("MD5 algorithm not found", e)
            }
        }
    }
}